import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:image_picker/image_picker.dart';
import 'package:mtf/app/base/BaseController.dart';
import 'package:mtf/app/net/HttpManger.dart';
import 'package:mtf/app/routes/app_pages.dart';
import 'package:path_provider/path_provider.dart' as path_provider;
import 'package:permission_handler/permission_handler.dart';
/**
 * GetX Template Generator - fb.com/htngu.99
 * */

class authenticationController extends BaseController {
  TextEditingController nameController = TextEditingController();
  TextEditingController phoneController = TextEditingController();
  TextEditingController cardController = TextEditingController();
  TextEditingController bankNameController = TextEditingController();
  TextEditingController bankNumController = TextEditingController();
  var idCardUp = "".obs;
  var idCardBack = "".obs;
  var bankFontBack = "".obs;


  void reqFileUpload(File file, int tag) async {
    print('reqFileUpload${file.path}');
    showLoading();
    var newFile = await compressAndGetFile(file);
    print('newFile${newFile.path}');
    var fileResp = await HttpManager.reqFileUpLoad(newFile);
    if (fileResp.isSuccess() && fileResp.data != null) {
      if (tag == 0) {
        idCardUp.value = fileResp.data!.fileNameUrl;
      } else if (tag == 1) {
        idCardBack.value = fileResp.data!.fileNameUrl;
      }else if(tag == 2){
        bankFontBack.value=fileResp.data!.fileNameUrl;
      }
    }
    hideLoading();
  }

  final _picker = ImagePicker();

  Future<void> selectImage(int tag) async {
    final pickedFile = await _picker.pickImage(source: ImageSource.gallery);
    if (pickedFile != null) {
      // 处理所选照片
      reqFileUpload(File(pickedFile.path), tag);
    }
    Get.back();
  }

  Future<void> selectCamera(int tag) async {
    //验证权限是否授权
    var status = await Permission.camera.status;
    if (status == PermissionStatus.granted) {
      //已经授权
      final pickedFile = await _picker.pickImage(source: ImageSource.camera);
      if (pickedFile != null) {
        // 处理所选照片
        reqFileUpload(File(pickedFile.path), tag);
      }
    } else {
      //最好是在调用openAppSettings();之前加一个弹窗说明用户没有对应权限需要去设置页面开启
      openAppSettings();
    }
    Get.back();
  }

  void reqAuth() async {
    var name = nameController.text;
    var phone = phoneController.text;
    var idCardNum = cardController.text;
    var bankName = bankNameController.text;
    var bankNum = bankNumController.text;

    if (name.isEmpty) {
      SmartDialog.showToast("请输入姓名");
    } else if (idCardNum.isEmpty) {
      SmartDialog.showToast("请输入身份证号");
    } else if (bankName.isEmpty) {
      SmartDialog.showToast("请输入开户行名称");
    } else if (bankNum.isEmpty) {
      SmartDialog.showToast("请输入银行卡号");
    } else if (idCardUp.isEmpty) {
      SmartDialog.showToast("请上传身份证人像面");
    } else if (idCardBack.isEmpty) {
      SmartDialog.showToast("请上传身份证人国徽面");
    } else if (bankFontBack.isEmpty) {
      SmartDialog.showToast("请上传身份证正面照");
    }else {
      showLoading();
      var resp = await HttpManager.reqAuth(
          name, idCardNum, bankName, bankNum, idCardUp.value, idCardBack.value,bankFontBack.value);
      if (resp.isSuccess()) {
        Get.offNamed(Routes.SUCCESS,arguments: {"title":"认证提交成功","desc":"等待平台审核，请耐心等候","type":1,"buttonText":"返回"});
      }
      hideLoading();
    }
  }

  /// 图片压缩 File -> File
  Future<File> compressAndGetFile(File file) async {
    final dir = await path_provider.getTemporaryDirectory();
    await dir.create(recursive: true);
    final targetPath =
        dir.absolute.path + '/${DateTime.now().microsecondsSinceEpoch}.jpg';
    var result = await FlutterImageCompress.compressAndGetFile(
      file.path,
      targetPath,
      quality: 88,
      rotate: 0,
    );
    return File(result!.path);
  }

  File createFile(String path) {
    final file = File(path);
    if (!file.existsSync()) {
      file.createSync(recursive: true);
    }
    return file;
  }
}
